
package tfoundation;

import java.awt.Point;
import java.awt.geom.Point2D;

public class Body
{
	private static final int DEF_SIZE_CLASS = 1;

	public static final int PIXEL_PER_SIZE = 16;

	private final Point realPos;
	private final Point backupPos;

	public String broadcastName = null;

	private int sizeClass;

	public double spdX = 0;
	public double spdY = 0;

	public double degree = 0;

	public boolean anchored = false;

	public Body()
	{
		this.realPos = new Point();
		this.backupPos = new Point();

		this.setSizeClass(DEF_SIZE_CLASS);
	}

	public String getBroadcastName()
	{
		return this.broadcastName;
	}

	public void giveInput()
	{

	}

	public void decideNextAction()
	{
		this.backupPos.setLocation(this.realPos);
		this.translate((int) this.spdX, (int) this.spdY);
	}

	public void rollback()
	{
		this.realPos.setLocation(this.backupPos);
	}

	public void setLocation(int x, int y)
	{
		this.realPos.setLocation(x, y);
	}

	public void translate(int dx, int dy)
	{
		this.realPos.x += dx;
		this.realPos.y += dy;
	}

	public int getSizeClass()
	{
		return this.sizeClass;
	}

	public boolean setSizeClass(int sizeClass)
	{
		boolean ok = true;

		if (ok)
		{
			this.sizeClass = sizeClass;
		}

		return ok;
	}

	public int getSize()
	{
		return this.sizeClass * Body.PIXEL_PER_SIZE;
	}

	public int getHalfSize()
	{
		return (this.sizeClass * Body.PIXEL_PER_SIZE) / 2;
	}

	public int getLocationX()
	{
		return this.realPos.x;
	}

	public int getLocationY()
	{
		return this.realPos.y;
	}

	public Point getLocation()
	{
		return new Point(this.realPos);
	}

	public void getLocation(Point buffer)
	{
		buffer.setLocation(this.realPos);
	}

	public double getSpeed()
	{
		return Math.sqrt(this.spdX * this.spdX + this.spdY * this.spdY);
	}

	public void correctOrientation()
	{
		if (this.degree < 0)
		{
			this.degree = 360 + this.degree;
		}
		else if (this.degree >= 360)
		{
			this.degree = this.degree % 360;
		}
	}

	public void speedVector(Point2D.Double buffer)
	{
		this.rotationIdentity(buffer);
		buffer.setLocation(buffer.x * this.spdX, buffer.y * this.spdY);
	}

	public void rotationIdentity(Point2D.Double buffer)
	{
		buffer.setLocation(Math.cos(Math.toRadians(this.degree)), Math.sin(Math.toRadians(this.degree)));
	}

	public double getDistance(Body body)
	{
		int diffX = body.realPos.x - this.realPos.x;
		int diffY = body.realPos.y - this.realPos.y;

		return Math.sqrt(diffX * diffX + diffY * diffY);
	}

	public boolean testCollision(Body body)
	{
		return this.getDistance(body) - this.getHalfSize() - body.getHalfSize() < 0;
	}
}
